package com.fingard.dsp.bank.directbank.cncbi01;

import java.util.Calendar;
import java.util.Date;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IDeposit;
import com.fingard.dsp.bank.reqrespobj.deposit.Req7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.RetAts7507DepQry;
import com.fingard.dsp.bank.reqrespobj.deposit.detail.RetAts7507Detail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class CNCBI01Deposit extends CNCBI01Base implements IDeposit {
    @Override
    public RetAts7507DepQry d_7507_DepQry(Req7507DepQry p_req7507) throws Exception {
        RetAts7507DepQry retAts = new RetAts7507DepQry(p_req7507.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        int QryCount = 0;
        while (QryCount < 5 && StringHelper.isNullOrEmpty(tmpRespCode)) {
            try {
                QryCount++;
                //赋值通用参数
                CNCBI01Header bankHeader = new CNCBI01Header();
                bankHeader.action = "DLFNDQRY";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeElementString("accountNo", p_req7507.actNum);//活期账号
                xmlWriter.writeElementString("qryFlg", "0");//查询标志 char(1)  0：普通查询；1：经办查询
                xmlWriter.writeElementString("openChanl", "0");//开户渠道char(1) 0：全部；1：柜台开户；2：网银开户
                xmlWriter.writeElementString("qryType", "0");//查询类型 char(1)  0：全部；1：未结清；2：已结清
                Calendar tmpCalendarFrom = Calendar.getInstance();
                tmpCalendarFrom.setTime(new Date());
                tmpCalendarFrom.add(Calendar.DATE, -QryCount * 365);
                tmpCalendarFrom.add(Calendar.DATE, 1);
                Calendar tmpCalendarTo = Calendar.getInstance();
                tmpCalendarTo.setTime(new Date());
                tmpCalendarTo.add(Calendar.DATE, -(QryCount - 1) * 365);
                xmlWriter.writeElementString("startDate", Format.Date8Format.format(tmpCalendarFrom.getTime()));//起始日期 char(8) 可空，格式YYYYMMDD
                xmlWriter.writeElementString("endDate", Format.Date8Format.format(tmpCalendarTo.getTime()));//截止日期 char(8) 可空，格式YYYYMMDD
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpStatus = tmpBkReader.getFirstText("/stream/status");
                        String tmpStatusText = tmpBkReader.getFirstText("/stream/statusText");
                        if (isActiveRetCode(tmpStatus)) {
                            while (tmpBkReader.read("/stream/list/row")) {
                                XElement readXElement = tmpBkReader.readedXElement;
                                RetAts7507Detail retAtsDetail = new RetAts7507Detail();
                                retAtsDetail.actNum = p_req7507.actNum;//银行账号
                                retAtsDetail.actArea = p_req7507.actArea;//账号地区区域码
                                retAtsDetail.depositCode = readXElement.getSingleTextNotNull("fixAccNo");//定期账号
                                String perd = readXElement.getSingleTextNotNull("perd");//存期 char(3)，M03：三个月；M06：六个月；Y01：一年；当查询账号为信银国际账号时，该值为天数
                                if ("M03".equals(perd)) {
                                    retAtsDetail.terms = "3";
                                } else if ("M06".equals(perd)) {
                                    retAtsDetail.terms = "4";
                                } else if ("Y01".equals(perd)) {
                                    retAtsDetail.terms = "5";
                                } else if ("Y02".equals(perd)) {
                                    retAtsDetail.terms = "6";
                                } else if ("Y03".equals(perd)) {
                                    retAtsDetail.terms = "7";
                                } else if ("Y05".equals(perd)) {
                                    retAtsDetail.terms = "8";
                                } else if (StringHelper.isNumber(perd)) {
                                    Integer perdInt = Integer.valueOf(perd);
                                    if (perdInt == 1) {
                                        retAtsDetail.terms = "0";
                                    } else if (perdInt == 7) {
                                        retAtsDetail.terms = "1";
                                    } else if (perdInt > 7 && perdInt <= 30) {
                                        retAtsDetail.terms = "2";
                                    } else if (perdInt > 30 && perdInt <= 90) {
                                        retAtsDetail.terms = "3";
                                    } else if (perdInt > 90 && perdInt <= 180) {
                                        retAtsDetail.terms = "4";
                                    } else if (perdInt > 180 && perdInt <= 360) {
                                        retAtsDetail.terms = "5";
                                    } else if (perdInt > 360 && perdInt <= 720) {
                                        retAtsDetail.terms = "6";
                                    } else if (perdInt > 720 && perdInt <= 1080) {
                                        retAtsDetail.terms = "7";
                                    } else if (perdInt > 1080 && perdInt <= 1800) {
                                        retAtsDetail.terms = "8";
                                    } else {
                                        retAtsDetail.terms = "9";
                                    }
                                } else {
                                    retAtsDetail.terms = "9";
                                }
                                retAtsDetail.valueDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("rateDate")));//起息日期 char(8) 格式YYYYMMDD
                                retAtsDetail.oriPrincipal = readXElement.getSingleTextNotNull("orbl");//原始本金 decimal(15，2)
                                retAtsDetail.noteMoney = readXElement.getSingleTextNotNull("balance");//账户余额 decimal(15，2)
                                retAtsDetail.rate = readXElement.getSingleTextNotNull("rate");//利率 decimal(9，7) ，返回值为百分数，例如返回3.05即，利率为3.05%
                                retAtsDetail.startDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("openDate")));//开户日期 char(8) 格式YYYYMMDD
                                retAtsDetail.endDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("dueDate")));//到期日 char(8) 格式YYYYMMDD
                                if (!StringHelper.isNullOrEmpty(readXElement.getSingleTextNotNull("cryType"))) {
                                    retAtsDetail.currencyID = convertCurToATS(readXElement.getSingleTextNotNull("cryType"));
                                }
                                retAts.writeRetDetail(retAtsDetail);
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpStatusText;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                }
            } catch (Exception e) {
                tmpRespCode = "-1";
                tmpRespInfo = e.getMessage();
                e.printStackTrace();
            }
        }
        if ("-1".equals(tmpRespCode)) {
            retAts = new RetAts7507DepQry(p_req7507.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else {
            tmpRespCode = "";
            tmpRespInfo = "";
            try {//赋值通用参数
                CNCBI01Header bankHeader = new CNCBI01Header();
                bankHeader.action = "DLNTYQRY";
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //写入通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                xmlWriter.writeElementString("accountNo", p_req7507.actNum);//活期账号
                xmlWriter.writeElementString("qryFlg", "0");//查询标志 char(1)  0：普通查询；1：经办查询
                xmlWriter.writeElementString("openChanl", "0");//开户渠道char(1) 0：全部；1：柜台开户；2：网银开户
                xmlWriter.writeElementString("qryType", "0");//查询类型 char(1)  0：全部；1：未结清；2：已结清
                Calendar tmpCalendarFrom = Calendar.getInstance();
                tmpCalendarFrom.setTime(new Date());
                tmpCalendarFrom.add(Calendar.DATE, -8);
                xmlWriter.writeElementString("startDate", Format.Date8Format.format(tmpCalendarFrom.getTime()));//起始日期 char(8) 可空，格式YYYYMMDD
                xmlWriter.writeElementString("endDate", Format.Date8Format.format(new Date()));//截止日期 char(8) 可空，格式YYYYMMDD
                xmlWriter.writeEndDocument();

                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());
                if (tmpStrRet[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        String tmpStatus = tmpBkReader.getFirstText("/stream/status");
                        String tmpStatusText = tmpBkReader.getFirstText("/stream/statusText");
                        if (isActiveRetCode(tmpStatus)) {
                            while (tmpBkReader.read("/stream/list/row")) {
                                XElement readXElement = tmpBkReader.readedXElement;
                                RetAts7507Detail retAtsDetail = new RetAts7507Detail();
                                retAtsDetail.actNum = p_req7507.actNum;//银行账号
                                retAtsDetail.actArea = p_req7507.actArea;//账号地区区域码
                                retAtsDetail.depositCode = readXElement.getSingleTextNotNull("notifyAccNo");//通知账号
                                String notifyType = readXElement.getSingleTextNotNull("notifyType");//通知类型char(2) 01：一天通知；02：七天通知，由通知存款查询返回
                                Date openDate = Format.Date8Format.parse(readXElement.getSingleTextNotNull("openDate"));
                                Calendar tmpCalendarTo = Calendar.getInstance();
                                tmpCalendarTo.setTime(openDate);
                                if ("01".equals(notifyType)) {
                                    retAtsDetail.terms = "0";
                                    tmpCalendarTo.add(Calendar.DATE, 1);
                                } else if ("02".equals(notifyType)) {
                                    retAtsDetail.terms = "1";
                                    tmpCalendarTo.add(Calendar.DATE, 7);
                                } else {
                                    retAtsDetail.terms = "9";
                                    tmpCalendarTo.add(Calendar.DATE, 7);
                                }
                                retAtsDetail.oriPrincipal = readXElement.getSingleTextNotNull("orbl");//原始本金 decimal(15，2)
                                retAtsDetail.noteMoney = readXElement.getSingleTextNotNull("balance");//账户余额 decimal(15，2)
                                retAtsDetail.startDate = Format.Date10Format.format(openDate);//开户日期 char(8) 格式YYYYMMDD
                                retAtsDetail.valueDate = Format.Date10Format.format(Format.Date8Format.parse(readXElement.getSingleTextNotNull("rateDate")));//起息日期 char(8) 格式YYYYMMDD
                                retAtsDetail.endDate = Format.Date10Format.format(tmpCalendarTo.getTime());
                                retAts.writeRetDetail(retAtsDetail);
                            }
                        } else {
                            tmpRespCode = "-1";
                            tmpRespInfo = tmpStatusText;
                        }
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpStrRet[1];
                }
            } catch (Exception e) {
                tmpRespCode = "-1";
                tmpRespInfo = e.getMessage();
                e.printStackTrace();
            }
            if ("-1".equals(tmpRespCode) && !retAts.hasWrittenRespCode) {
                retAts = new RetAts7507DepQry(p_req7507.reqHeader);
                retAts.writeStart();//写返回ATS的统一头部信息
                retAts.writeRespCode(tmpRespCode);
                retAts.writeRespInfo(tmpRespInfo);
            } else if (retAts.getWrittenRdCount() == 0) {
                retAts.writeRespCode("0");
                retAts.writeRespInfo("查无存单");
            }
        }
        retAts.writeEnd();
        return retAts;
    }
}
